package com.ncmmall.mall.controller.business.goods;

import com.ncmmall.application.business.goods.GoodsChannelApplication;
import com.ncmmall.application.business.goods.GoodsCommentApplication;
import com.ncmmall.application.securitys.logs.OperationLogApplication;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.goods.comment.GoodsComment;
import com.ncmmall.domain.business.goods.comment.QGoodsComment;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 商品评价
 */
@Controller
@RequestMapping(value = "/mall/goods/comment")
public class GoodsCommentController {

    private final static Logger logger = LoggerFactory.getLogger(GoodsCommentController.class);

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private GoodsChannelApplication channelApplication;

    @Autowired
    private GoodsCommentApplication goodsCommentApplication;

    @Autowired
    private OperationLogApplication logApplication;

    @Value("${web.domain}")
    private String webDomain;

    /**
     * 商品评论列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, GoodsComment params) {

        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());
        Map<String, Object> map = new HashMap<>();

        String hql = buildSql("select c from GoodsComment c where 1 = 1", map, params);

        QueryPage<GoodsComment> page = queryChannelService.findPage(hql, pageRequest.setParams(map), GoodsComment.class);

        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/goods/comment/list";
    }

    private String buildSql(String sql, Map<String, Object> map, GoodsComment params) {


        sql += " and c.platformType = :type ";
        map.put("type", params.getPlatformType());

        if (params.getMember() != null) {
            if (StringUtils.isNotBlank(params.getMember().getMobile())) {
                sql += " and c.member.mobile = :mobile";
                map.put("mobile", "%" + params.getMember().getMobile());
            }
        }


        if (params.getGoods() != null) {

            if (StringUtils.isNotBlank(params.getGoods().getName())) {
                sql += " and c.goods.name like :name";
                map.put("name", "%" + params.getGoods().getName() + "%");
            }
        }


        if (params.getMerchant() != null) {

            if (StringUtils.isNotBlank(params.getMerchant().getRealName())) {
                sql += " and c.merchant.realName like :realName";
                map.put("realName", "%" + params.getMerchant().getRealName() + "%");
            }
        }

        if (params.getStatus() > 0) {
            sql += " and c.status = :status";
            map.put("status", params.getStatus());
        }

        sql += " order by c.createTime desc";

        return sql;
    }


    /**
     * 商品评论详情
     *
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model, long id) {

        GoodsComment goodsComment = queryChannelService.findOne(QGoodsComment.goodsComment.id.eq(id), GoodsComment.class);

        model.addAttribute("goodsComment", goodsComment);

        return "mall/goods/comment/detail";
    }


    /**
     * 下架
     *
     * @param requestWith
     * @param news
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> disable(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, GoodsComment goodsComment) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }

            goodsComment = queryChannelService.findOne(QGoodsComment.goodsComment.id.eq(goodsComment.getId()), GoodsComment.class);
            goodsCommentApplication.disable(goodsComment);

            logApplication.eventOfSystem(Securitys.currentUser(), "下架商品评论", Securitys.getIp(request));

            return ImmutableMap.of("status", true, "message", "下架成功");

        } catch (Exception e) {

            logger.error("评价下架异常", e);
            return ImmutableMap.of("status", false, "message", "下架异常");

        }
    }

    /**
     * 上架
     *
     * @param requestWith
     * @param news
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> enable(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, GoodsComment goodsComment) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            goodsComment = queryChannelService.findOne(QGoodsComment.goodsComment.id.eq(goodsComment.getId()), GoodsComment.class);
            goodsCommentApplication.enable(goodsComment);
            logApplication.eventOfSystem(Securitys.currentUser(), "上架商品评论", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "上架成功");

        } catch (Exception e) {

            logger.error("评价上架异常", e);
            return ImmutableMap.of("status", false, "message", "上架异常");

        }
    }


}
